package cn.elwy.rcp.ui.widget;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * 自定义Tab面板
 * 
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class CTabComposite extends Composite {

  private int itemCount;
  private int selectedIndex = -1;
  List<CTabButton> items = new ArrayList<CTabButton>();
  private Composite navComposite;
  private Composite itemComposite;
  private CTabButton selectedItem;
  private StackLayout stackLayout;

  public int getItemCount() {
    return itemCount;
  }

  /**
   * Create the composite.
   * 
   * @param parent
   * @param style
   */
  public CTabComposite(Composite parent, int style) {
    super(parent, style);
    setLayout(new FormLayout());
    // int treeTop = 0;
    // int treeBottom = 100;
    // int treeLeft = 0;
    // int treeRithg = 0;

    navComposite = new Composite(this, SWT.NONE);
    navComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
    GridLayout gridLayout = new GridLayout(1, false);
    gridLayout.marginWidth = 0;
    navComposite.setLayout(gridLayout);
    FormData fd_navComposite = new FormData();
    fd_navComposite.top = new FormAttachment(0);
    fd_navComposite.bottom = new FormAttachment(100);
    fd_navComposite.left = new FormAttachment(0);
    fd_navComposite.right = new FormAttachment(0, 152);
    navComposite.setLayoutData(fd_navComposite);

    // Button btnNewButton = new Button(navComposite, SWT.NONE);
    // GridData g =new GridData();
    // g.grabExcessHorizontalSpace = true;
    // g.horizontalAlignment = SWT.FILL;
    // btnNewButton.setLayoutData(g);
    // btnNewButton.setText("New Button");

    // Button btnNewButton = new Button(navComposite, SWT.NONE);
    // GridData g = new GridData();
    // g.grabExcessHorizontalSpace = true;
    // g.horizontalAlignment = SWT.FILL;
    // btnNewButton.setLayoutData(g);
    // btnNewButton.setText("New Button");
    //
    // Button btnNewButton_1 = new Button(navComposite, SWT.NONE);
    // btnNewButton_1.setText("New Button");

    itemComposite = new Composite(this, SWT.BORDER);
    stackLayout = new StackLayout();
    itemComposite.setLayout(stackLayout);
    FormData fd_itemComposite = new FormData();
    fd_itemComposite.left = new FormAttachment(0, 150);
    fd_itemComposite.right = new FormAttachment(100);
    fd_itemComposite.bottom = new FormAttachment(100);
    fd_itemComposite.top = new FormAttachment(0);
    itemComposite.setLayoutData(fd_itemComposite);
    itemComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);

  }

  public Composite getNavComposite() {
    return navComposite;
  }

  public void setNavComposite(Composite navComposite) {
    this.navComposite = navComposite;
  }

  public Composite getItemComposite() {
    return itemComposite;
  }

  public void setItemComposite(Composite itemComposite) {
    this.itemComposite = itemComposite;
  }

  @Override
  protected void checkSubclass() {
  }

  public void createItem(CTabButton item, int index) {
    if (0 > index || index > getItemCount())
      SWT.error(SWT.ERROR_INVALID_RANGE);
    item.parent = this;
    item.setParent(navComposite);
    GridData gridData = (GridData) item.getLayoutData();
    if (gridData == null) {
      gridData = new GridData();
    }
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalAlignment = SWT.FILL;
    item.setLayoutData(gridData);

    item.addSelectionListener(new SelectionListener() {

      @Override
      public void widgetSelected(SelectionEvent e) {
        CTabButton cTabTutton = (CTabButton) e.widget;
        setChecked(cTabTutton);

      }

      @Override
      public void widgetDefaultSelected(SelectionEvent selectionevent) {

      }
    });
    items.add(index, item);
    updateItems();
    itemCount++;
  }

  public void setChecked(CTabButton item) {
    if (selectedItem != null) {
      selectedItem.setEnabled(true);
    }
    stackLayout.topControl = item.getControl();
    itemComposite.layout();
    item.setEnabled(false);
    item.setFocus();
    selectedItem = item;
    selectedIndex = indexOf(item);
  }

  public void update(CTabButton item) {
    Control control = item.getControl();
    if (control != null) {
      ((Composite) control).setBackgroundMode(SWT.INHERIT_DEFAULT);
      control.setParent(itemComposite);
    }
  }

  private void updateItems() {
    layout();
    for (CTabButton cTabItem : items) {
      cTabItem.update();
    }
  }

  public int indexOf(CTabButton item) {
    checkWidget();
    if (item == null) {
      return -1;
    }
    for (int i = 0; i < itemCount; i++) {
      if (items.get(i) == item) {
        return i;
      }
    }
    return -1;
  }

  public int getSelectedIndex() {
    return selectedIndex;
  }

  public CTabButton getSelectedItem() {
    return getItem(selectedIndex);
  }

  public CTabButton getItem(int index) {
    if (index < 0 || index >= items.size()) {
      return null;
    }
    return items.get(index);
  }

}
